/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.CityResourcesDao;
import cz.muni.fi.diplomka.datalayer.entity.CityResourcesEntity;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class CityResourcesDaoImpl implements CityResourcesDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public CityResourcesDaoImpl() {
    }

    @Override
    public void addResourcesInCity(CityResourcesEntity resourcesInCityEntity) {
        if (resourcesInCityEntity == null) {
            throw new IllegalArgumentException("ResourcesInCityEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO city_resources"
                    + "(city_id, resource, resource_count) VALUES"
                    + "(?,?,?)";
            ps = con.prepareStatement(insert);


            ps.setLong(1, resourcesInCityEntity.getCityId());
            ps.setString(2, resourcesInCityEntity.getResource().name());
            ps.setInt(3, resourcesInCityEntity.getResourceCount());

            ps.executeUpdate();

            rs = ps.getGeneratedKeys();
            rs.next();

        } catch (SQLException ex) {
            Logger.getLogger(CityResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editResourcesInCity(CityResourcesEntity resourcesInCityEntity) {
        if (resourcesInCityEntity == null) {
            throw new IllegalArgumentException("ResourcesInCityEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE city_resources SET resource_count=? WHERE city_id=? AND resource=?";

            ps = con.prepareStatement(update);

            ps.setInt(1, resourcesInCityEntity.getResourceCount());
            ps.setLong(2, resourcesInCityEntity.getCityId());
            ps.setString(3, resourcesInCityEntity.getResource().name());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(CityResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<CityResourcesEntity> getAllResourcesInCityOfPlayer(Long cityId) {
        if (cityId == null) {
            throw new IllegalArgumentException("City ID can not be null");
        }
        List<CityResourcesEntity> resourcesInCityEntityList = new ArrayList<CityResourcesEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllResourcesInCityOfPlayer = "SELECT * FROM city_resources WHERE city_id=?";

            ps = con.prepareStatement(getAllResourcesInCityOfPlayer);
            ps.setLong(1, cityId);

            rs = ps.executeQuery();

            while (rs.next()) {
                CityResourcesEntity resourcesInCityEntity = new CityResourcesEntity();
                resourcesInCityEntity.setCityId(rs.getLong("city_id"));
                resourcesInCityEntity.setResource(ResourceType.valueOf(rs.getString("resource")));
                resourcesInCityEntity.setResourceCount(rs.getInt("resource_count"));
                resourcesInCityEntityList.add(resourcesInCityEntity);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CityResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return resourcesInCityEntityList;
    }

    @Override
    public int getAllSpecificResourcesCountInCityOfPlayer(Long cityId, ResourceType resourceType) {
        if (cityId == null) {
            throw new IllegalArgumentException("City ID can not be null");
        }
        if (resourceType == null) {
            throw new IllegalArgumentException("Resource Type can not be null");
        }
        CityResourcesEntity resourcesInCityEntity = new CityResourcesEntity();

        try {
            con = DbConnector.getDBConnection();
            String getAllSpecificResourcesCountInCityOfPlayer = "SELECT * FROM city_resources WHERE city_id=? AND resource=?";

            ps = con.prepareStatement(getAllSpecificResourcesCountInCityOfPlayer);
            ps.setLong(1, cityId);
            ps.setString(2, resourceType.toString());

            rs = ps.executeQuery();

            while (rs.next()) {
                resourcesInCityEntity.setCityId(rs.getLong("city_id"));
                resourcesInCityEntity.setResource(ResourceType.valueOf(rs.getString("resource")));
                resourcesInCityEntity.setResourceCount(rs.getInt("resource_count"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(CityResourcesDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return resourcesInCityEntity.getResourceCount();
    }
}
